class Ems::StoreStaffSalariesController < Ems::BaseController
  before_action :set_salary, :set_staff, only: [:show, :edit, :update]
  def index
    set_search_params
    @current_month = Time.now.last_month.strftime("%Y年%m月")
    @q = current_store.store_staffs.available.includes(:salaries, :store_department, :store_position).ransack(params[:q])
    @store_staffs = @q.result.order("id asc")
    # @actual_payment_amount = @store_staffs.map(&->(staff){ staff.review_salary.actual_amount}).sum()
  end

  def show
  end

  def edit

  end

  def update
    @staff.update!(staff_params)
    @salary.update!(salary_params) unless @salary.salary_status.present?
    redirect_to ems_store_staff_salary_path(@salary)
  end

  private
  def set_salary
    @salary = StoreStaffSalary.find(params[:id])
  end

  def set_staff
    @staff = @salary.store_staff
  end

  def staff_params
    params.require(:store_staff).permit(
      :commission,
      subsidy: [:gangwei, :zhusu, :canfei, :laobao, :gaowen, :other],
      insurance: [:yiliao, :yanglao, :shiye, :gongjijin]
    )
  end

  def salary_params
    params[:store_staff][:subsidy_amount] = @staff.subsidy_amount
    params[:store_staff][:insurance_amount] = @staff.insurance_amount
    params.require(:store_staff).permit(
      :commission,
      :subsidy_amount,
      :insurance_amount,
      subsidy: [:gangwei, :zhusu, :canfei, :laobao, :gaowen, :other],
      insurance: [:yiliao, :yanglao, :shiye, :gongjijin]
    )
  end

  def set_search_params
    params[:q] ||= {}
    @name = params[:q][:full_name_cont]
  end
end
